package com.ffcc.fitness.websocket.handler;

import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;

/**
 * WebSocket测试处理器 - 不需要token验证
 */
@Component
@ServerEndpoint("/websocket/test/{userId}")
public class WebSocketTestHandler {
    
    @OnOpen
    public void onOpen(Session session, @PathParam("userId") String userId) {
        System.out.println("=== WebSocket测试连接成功 ===");
        System.out.println("测试用户ID: " + userId);
        System.out.println("测试Session ID: " + session.getId());
        System.out.println("测试Request URI: " + session.getRequestURI());
        
        try {
            session.getBasicRemote().sendText("测试连接成功！用户ID: " + userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    @OnMessage
    public void onMessage(String message, Session session, @PathParam("userId") String userId) {
        System.out.println("=== 收到测试消息 ===");
        System.out.println("用户ID: " + userId);
        System.out.println("消息: " + message);
        
        try {
            session.getBasicRemote().sendText("回复: " + message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    @OnClose
    public void onClose(@PathParam("userId") String userId) {
        System.out.println("=== WebSocket测试连接关闭 ===");
        System.out.println("用户ID: " + userId);
    }
    
    @OnError
    public void onError(Session session, Throwable error, @PathParam("userId") String userId) {
        System.err.println("=== WebSocket测试连接错误 ===");
        System.err.println("用户ID: " + userId);
        System.err.println("错误: " + error.getMessage());
        error.printStackTrace();
    }
}
